<?php

namespace app\admin\command;

use fast\Http;
use think\console\Input;
use think\console\Output;
use think\console\Command;
use app\admin\model\wanlshop\Brand;
use app\admin\model\wanlshop\Category;
use app\admin\model\wanlshop\ShopSort;

class Cate extends Command
{
    protected function configure()
    {
        $this->setName('cate')
            ->setDescription('sync shop category data');
    }

    protected function execute(Input $input, Output $output)
    {
        date_default_timezone_set('PRC');
        ini_set('memory_limit', '-1');
        set_time_limit(0);

        var_dump('get list');
        $this->syncCategory($output);
        $this->syncBrand(1, 100, $output);
    }

    protected function syncCategory($output)
    {
        //todo 暂时只取一级分类
        $params = [
            'c_level' => 1
        ];

        // header
        $header = [
//            'channelType:' . 'DONGHUOMUSZ',
            'channelType:' . 'DONGHUOMU',
            'md5:' . $this->getSign($params)
        ];

        // 额外参数
        $option = [
            CURLOPT_HTTPHEADER => $header,
            CURLOPT_POSTFIELDS => http_build_query($params),
            CURLOPT_POST => 1
        ];

//        $resp = Http::post("https://uat.api.weipinshang.net/mcang/Mcang/getGoodsClassify", $params, $option);
        $resp = Http::post("https://api.weipinshang.net/mcang/Mcang/getGoodsClassify", $params, $option);
        $list = json_decode($resp, true);
        if (isset($list['code']) && $list['code'] != 0) {
            $output->writeln("请求分类数据失败,msg:{$list['msg']}");
            return true;
        }

        foreach ($list['data'] as $item) {
            $flag = '更新';
            $category = Category::where('name', $item['c_name'])->find();
            if (!$category) {
                $flag = '新增';
                $category = new Category();
            }
            $category->pid = 0;
            $category->type = 'goods';
            $category->name = $item['c_name'];
            $category->c_id = $item['c_id'];
            $category->c_level = $item['c_level'];
            $category->c_code = $item['c_code'];
            $category->c_parent_code = $item['c_parent_code'];
            $category->save();

            $sort = ShopSort::where('name', $item['c_name'])->find();
            if (!$sort) {
                $sort = new ShopSort();
            }
            $sort->shop_id = 10;
            $sort->name = $item['c_name'];
            $sort->createtime = time();
            $sort->updatetime = time();
            $sort->save();
            $output->writeln("「{$flag}」分类数据,id:{$category->id},name：{$category->name},--{$item['c_id']}");
        }
    }

    protected function syncBrand($page, $size, $output)
    {
        while ($page) {
            $params = [
                'pageNo' => $page,
                'pageSize' => $size,
            ];

            // header
            $header = [
                'channelType:' . 'DONGHUOMU',
//                'channelType:' . 'DONGHUOMU',
                'md5:' . $this->getSign($params)
            ];

            // 额外参数
            $option = [
                CURLOPT_HTTPHEADER => $header,
                CURLOPT_POSTFIELDS => http_build_query($params),
                CURLOPT_POST => 1
            ];

//            $resp = Http::post("https://uat.api.weipinshang.net/mcang/Mcang/goodBrand", $params, $option);
            $resp = Http::post("https://api.weipinshang.net/mcang/Mcang/goodBrand", $params, $option);
            $list = json_decode($resp, true);
            if (isset($list['code']) && $list['code'] != 0) {
                $output->writeln("请求品牌数据失败,msg:{$list['msg']}");
                return true;
            }

            if (empty($list['data']['list'])) {
                return true;
            }
            foreach ($list['data']['list'] as $item) {
                $flag = '更新';
                $brand = Brand::where('name', $item['c_brand_name'])->find();
                if (!$brand) {
                    $flag = '新增';
                    $brand = new Brand();
                }
                $brand->shop_id = 10;
                $brand->name = $item['c_brand_name'] ?? '';
                $brand->image = $item['c_brand_logo_url'] ?? '';
                $brand->c_id = $item['c_id'] ?? 0;
                $brand->switch = 1;
                $brand->state = 1;
                $brand->save();
                $output->writeln("「{$flag}」品牌数据,id:{$brand->id},name：{$brand->name}");
            }
            ++$page;
        }
        return true;
    }

    protected function getSign($params = [])
    {
        $query = [];
        ksort($params);
        foreach ($params as $key => $val) {
            $query[] = $key . "=" . $val;
        }
        $data = implode('&', $query);
//        return strtolower(md5($data . "DONGHUOMUSZ" . "4e0f57288842aa18f236b45f72d1e2a8"));//测试
        return strtolower(md5($data . "DONGHUOMU" . "ca69a0343ac66303f01d172bb86bd65c"));//正式
    }
}